package pers.cj.pdos.controller.analysis;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.BusinessSet;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.InGoodsRefundAnalysis;
import pers.cj.pdos.excel.inGoodsRefundAnalysis.*;
import pers.cj.pdos.service.*;
import pers.cj.pdos.task.CleanTempExcelTask;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.FileProperUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.InGoodsRefundGoodsVo;
import pers.cj.pdos.vo.InGoodsRefundVo;
import pers.cj.pdos.vo.UserVo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goodsCount")
public class InGoodsRefundAnalysisController extends BaseController {
    @Autowired
    IInGoodsRefundAnalysisService inGoodsRefundAnalysisService;
    @Autowired
    IStoreService storeService;
    @Autowired
    IHouseService houseService;
    @Autowired
    IGoodsService goodsService;
    @Autowired
    IProviderService providerService;
    @Autowired
    IInGoodsRefundService inGoodsRefundService;


    @RequiresPermissions("inGoodsRefundAnalysis:query")
    @RequestMapping("/inGoodsRefundAnalysis")
    public String inGoodsRefundAnalysis() {
        return "analysis/inGoodsRefund/inGoodsRefundAnalysis";
    }

    @RequiresPermissions("inGoodsRefundAnalysis:query")
    @RequestMapping("/inGoodsRefundAnalysisDetailGoods")
    public String refunAnalysisDetailGoods(Model model, String goodsId,String storeId,String houseId, String date1, String date2) {
        model.addAttribute("goodsId",goodsId);
        model.addAttribute("storeId",storeId);
        model.addAttribute("houseId",houseId);
        model.addAttribute("date1",date1);
        model.addAttribute("date2",date2);

        model.addAttribute("goodsVo",  goodsService.getVoById(goodsId));

        if (!Utils.isEmpty(storeId)) {
            model.addAttribute("storeName",storeService.getById(storeId).getName());
        }

        if (!Utils.isEmpty(houseId)) {
            model.addAttribute("houseName",houseService.getById(houseId).getName());
        }

        return "analysis/inGoodsRefund/inGoodsRefundAnalysisDetailGoods";
    }

    @RequiresPermissions("inGoodsRefundAnalysis:query")
    @RequestMapping("/inGoodsRefundAnalysisDetailProvider")
    public String inGoodsRefundAnalysisDetailProvider(Model model, String providerId,String storeId,String houseId, String date1, String date2) {
        model.addAttribute("providerId",providerId);
        model.addAttribute("storeId",storeId);
        model.addAttribute("houseId",houseId);
        model.addAttribute("date1",date1);
        model.addAttribute("date2",date2);

        model.addAttribute("provider",  providerService.getById(providerId));

        if (!Utils.isEmpty(storeId)) {
            model.addAttribute("storeName",storeService.getById(storeId).getName());
        }

        if (!Utils.isEmpty(houseId)) {
            model.addAttribute("houseName",houseService.getById(houseId).getName());
        }

        return "analysis/inGoodsRefund/inGoodsRefundAnalysisDetailProvider";
    }

    @RequiresPermissions("inGoodsRefundAnalysis:query")
    @RequestMapping("/inGoodsRefundAnalysisDetailOrder")
    public String inGoodsRefundAnalysisDetailOrder(Model model, String storeId,String houseId, String date) {
        model.addAttribute("date",date);
        model.addAttribute("storeId",storeId);
        model.addAttribute("houseId",houseId);

        if (!Utils.isEmpty(storeId)) {
            model.addAttribute("storeName",storeService.getById(storeId).getName());
        }

        if (!Utils.isEmpty(houseId)) {
            model.addAttribute("houseName",houseService.getById(houseId).getName());
        }

        return "analysis/inGoodsRefund/inGoodsRefundAnalysisDetailOrder";
    }


    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/getAllTotalData")
    public ResponseData getAllTotalData(String storeId,
                                        String houseId,
                                        String date1,
                                        String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        Map map = inGoodsRefundAnalysisService.getTotalData(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId), houseId, Utils.genDate(date1), Utils.genDate(date2));

        Map highGoods = inGoodsRefundAnalysisService.getHighGoods(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId), houseId, Utils.genDate(date1), Utils.genDate(date2));
        Map highProvider = inGoodsRefundAnalysisService.getHighProvider(company.getId(), PdosUtils.getViewOtherStoreId(userVo,storeId), houseId, Utils.genDate(date1), Utils.genDate(date2));

        map.put("highGoods", highGoods);
        map.put("highProvider", highProvider);
        return ResponseData.success(map);
    }


    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/listStoreData")
    public ResponseList listStoreData(String storeId,
                                      String houseId,
                                      String date1,
                                      String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listStoreData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        return ResponseList.success(list.size(), list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/listHouseData")
    public ResponseList listHouseData(String storeId,
                                      String houseId,
                                      String date1,
                                      String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listHouseData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        return ResponseList.success(list.size(), list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/listGoodsData")
    public ResponseList listGoodsData(String page,
                                     String limit,
                                     String storeId,
                                     String houseId,
                                      String providerId,
                                     String date1,
                                     String date2,
                                     String typeId,
                                     String filterZero,
                                     String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listGoodsData(company.getId(),
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                providerId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                typeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        Long count = inGoodsRefundAnalysisService.countGoodsData(company.getId(),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                providerId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                typeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/listProviderData")
    public ResponseList listProviderData(String page,
                                      String limit,
                                      String storeId,
                                      String houseId,
                                      String date1,
                                      String date2,
                                      String providerTypeId,
                                      String filterZero,
                                      String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listProviderData(company.getId(),
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                providerTypeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        Long count = inGoodsRefundAnalysisService.countProviderData(company.getId(),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                providerTypeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/listOrderData")
    public ResponseList listOrderData(String page,
                                     String limit,
                                     String storeId,
                                     String houseId,
                                     String date1,
                                     String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listOrderData(company.getId(),
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        Long count = inGoodsRefundAnalysisService.countOrderData(company.getId(),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/getOrderDetail")
    public ResponseList getOrderDetail(int page,
                                int limit,
                                String close,
                                String search,
                                String no,
                                String goodsName,
                                String remark,
                                String provider,
                                String makeTime1,
                                String makeTime2,
                                String storeId,
                                String userId,
                                String date1,
                                String date2,
                                String date,
                                String providerId,
                                String houseId,
                                String makeUserId){
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();

        List<InGoodsRefundVo> list = inGoodsRefundService.listVo(company.getId(),
                page,
                limit,
                Utils.genBoolean(close),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(provider),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                PdosUtils.getViewOtherStoreId(userVo, storeId),
                null,
                userId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genDate(date),
                providerId,
                houseId,
                makeUserId);
        long count = inGoodsRefundService.count(company.getId(),
                Utils.genBoolean(close),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(provider),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                PdosUtils.getViewOtherStoreId(userVo, storeId),
                null,
                userId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genDate(date),
                providerId,
                houseId,
                makeUserId);
        return ResponseList.success(count,list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/listGoodsDetailDataByOrder")
    public ResponseList listGoodsDetailDataByOrder(String goodsId,
                                            String page,
                                            String limit,
                                            String storeId,
                                            String houseId,
                                            String date1,
                                            String date2,
                                            String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listGoodsDetailDataByOrder(company.getId(),
                goodsId,
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        Long count = inGoodsRefundAnalysisService.countGoodsDetailDataByOrder(company.getId(),
                goodsId,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/listGoodsDetailDataByProvider")
    public ResponseList listGoodsDetailDataByProvider(String goodsId,
                                                   String page,
                                                   String limit,
                                                   String storeId,
                                                   String houseId,
                                                   String date1,
                                                   String date2,
                                                   String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listGoodsDetailDataByProvider(company.getId(),
                goodsId,
                Utils.genInteger(page),
                Utils.genInteger(limit),
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        Long count = inGoodsRefundAnalysisService.countGoodsDetailDataByProvider(company.getId(),
                goodsId,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        return ResponseList.success(count, list);
    }

    @ResponseBody
    @RequestMapping("/inGoodsRefundAnalysis/chatDetailData")
    public ResponseData chatDetailData(String goodsId,
                                      String providerId,
                                      String storeId,
                                      String houseId,
                                      String date1,
                                      String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        if (Utils.isEmpty(date1) || Utils.isEmpty(date2)) {
            //则日期需要根据数据库出来的数据做判断
            Map map = inGoodsRefundAnalysisService.getMaxMinDate(company.getId(), goodsId, providerId, PdosUtils.getViewOtherStoreId(userVo,storeId),houseId);
            if (map == null) {
                //说明数据库无数据
                return ResponseData.success(null);
            }

            date1 = DateUtils.getDate((Date)map.get("minDate"), DateUtils.YYYY_MM_DD);
            date2 = DateUtils.getDate((Date)map.get("maxDate"), DateUtils.YYYY_MM_DD);
        }

        int flag = PdosUtils.calcDateDiffFlag(date1,date2);
        if (flag == -1) {
            return ResponseData.success(null);
        }

        Map<String, Object> chatMap = PdosUtils.calcDateDiffMap(date1,date2, businessSet);
        if (flag == 1) {
            //按照年份获取总进货退货额
            List<Map> mapList = inGoodsRefundAnalysisService.getTotalByYear(company.getId(),goodsId, providerId,PdosUtils.getViewOtherStoreId(userVo,storeId),houseId, Utils.genDate(date1), Utils.genDate(date2));
            for (Map map : mapList) {
                chatMap.put(map.get("year")+"年", PdosUtils.getPrice((BigDecimal) map.get("amount"),businessSet));
            }
        } else if (flag == 2) {
            //按照月获取总进货退货额
            List<Map> mapList = inGoodsRefundAnalysisService.getTotalByMonth(company.getId(),goodsId, providerId,PdosUtils.getViewOtherStoreId(userVo,storeId),houseId, Utils.genDate(date1), Utils.genDate(date2));
            for (Map map : mapList) {
                chatMap.put(map.get("month")+"月", PdosUtils.getPrice((BigDecimal) map.get("amount"),businessSet));
            }
        } else if (flag == 3) {
            //按照日获取总进货退货额
            List<Map> mapList = inGoodsRefundAnalysisService.getTotalByDate(company.getId(),goodsId, providerId,PdosUtils.getViewOtherStoreId(userVo,storeId),houseId, Utils.genDate(date1), Utils.genDate(date2));
            for (Map map : mapList) {
                chatMap.put(map.get("date")+"日", PdosUtils.getPrice((BigDecimal) map.get("amount"),businessSet));
            }
        }

        return ResponseData.success(chatMap);
    }




    @ResponseBody
    @RequiresPermissions("inGoodsRefundAnalysis:export")
    @RequestMapping("/inGoodsRefundAnalysis/exportGoodsData")
    public ResponseData exportGoodsData(String fileName,
                                        String storeId,
                                        String houseId,
                                        String providerId,
                                        String date1,
                                        String date2,
                                        String typeId,
                                        String filterZero,
                                        String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listGoodsData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                providerId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                typeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        List<ExcelInGoodsRefundAnalysisGoods> excelList = new ArrayList<>();
        for (InGoodsRefundAnalysis entity : list) {
            ExcelInGoodsRefundAnalysisGoods excel = new ExcelInGoodsRefundAnalysisGoods();
            excel.setNo(entity.getNo());
            excel.setName(entity.getName());
            excel.setNorms(entity.getNorms());
            excel.setUnitName(entity.getUnitName());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setAvgAmount(PdosUtils.getPrice(entity.getAvgAmount(), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }


        String excelName = fileName + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";

        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + excelName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsRefundAnalysisGoods.class);
            sheet1.setSheetName(fileName + "信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(excelName, new Date().getTime());
            return ResponseData.success(excelName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsRefundAnalysis:export")
    @RequestMapping("/inGoodsRefundAnalysis/exportProviderData")
    public ResponseData exportProviderData(String fileName,
                                         String storeId,
                                         String houseId,
                                         String date1,
                                         String date2,
                                         String providerTypeId,
                                         String filterZero,
                                         String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listProviderData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                providerTypeId,
                Utils.genBoolean(filterZero),
                Utils.genLikeData(search));

        List<ExcelInGoodsRefundAnalysisProvider> excelList = new ArrayList<>();
        for (InGoodsRefundAnalysis entity : list) {
            ExcelInGoodsRefundAnalysisProvider excel = new ExcelInGoodsRefundAnalysisProvider();
            excel.setNo(entity.getNo());
            excel.setName(entity.getName());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setRatio(PdosUtils.getNumber(entity.getRatio(), businessSet)+"%");
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String excelName = fileName + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + excelName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsRefundAnalysisProvider.class);
            sheet1.setSheetName(fileName + "信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(excelName, new Date().getTime());
            return ResponseData.success(excelName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsRefundAnalysis:export")
    @RequestMapping("/inGoodsRefundAnalysis/exportOrderData")
    public ResponseData exportOrderData(String fileName,
                                        String storeId,
                                        String houseId,
                                        String date1,
                                        String date2) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listOrderData(company.getId(),
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        List<ExcelInGoodsRefundAnalysisOrder> excelList = new ArrayList<>();
        for (InGoodsRefundAnalysis entity : list) {
            ExcelInGoodsRefundAnalysisOrder excel = new ExcelInGoodsRefundAnalysisOrder();
            excel.setDate(entity.getDate());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String excelName = fileName + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + excelName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsRefundAnalysisOrder.class);
            sheet1.setSheetName(fileName+"信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(excelName, new Date().getTime());
            return ResponseData.success(excelName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsRefundAnalysis:export")
    @RequestMapping("/inGoodsRefundAnalysis/exportGoodsDetailDataByOrder")
    public ResponseData exportGoodsDetailDataByOrder(String goodsId,
                                                     String storeId,
                                                     String houseId,
                                                     String date1,
                                                     String date2,
                                                     String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listGoodsDetailDataByOrder(company.getId(),
                goodsId,
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        List<ExcelInGoodsRefundAnalysisGoodsOrder> excelList = new ArrayList<>();
        for (InGoodsRefundAnalysis entity : list) {
            ExcelInGoodsRefundAnalysisGoodsOrder excel = new ExcelInGoodsRefundAnalysisGoodsOrder();
            excel.setDate(entity.getDate());
            excel.setNo(entity.getNo());
            excel.setProviderName(entity.getProviderName());
            excel.setUnitName(entity.getUnitName());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setAvgAmount(PdosUtils.getPrice(entity.getAmount().divide(entity.getNumber(),4, RoundingMode.HALF_UP), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String fileName = "进货退货报表报表按商品按单据表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsRefundAnalysisGoodsOrder.class);
            sheet1.setSheetName("进货退货报表按商品按单据表信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName, new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsRefundAnalysis:export")
    @RequestMapping("/inGoodsRefundAnalysis/exportGoodsDetailDataByProvider")
    public ResponseData exportGoodsDetailDataByProvider(String goodsId,
                                                      String storeId,
                                                      String houseId,
                                                      String date1,
                                                      String date2,
                                                      String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsRefundAnalysis> list = inGoodsRefundAnalysisService.listGoodsDetailDataByProvider(company.getId(),
                goodsId,
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                houseId,
                Utils.genDate(date1),
                Utils.genDate(date2),
                Utils.genLikeData(search));

        List<ExcelInGoodsRefundAnalysisGoodsProvider> excelList = new ArrayList<>();
        for (InGoodsRefundAnalysis entity : list) {
            ExcelInGoodsRefundAnalysisGoodsProvider excel = new ExcelInGoodsRefundAnalysisGoodsProvider();
            excel.setName(entity.getName());
            excel.setUnitName(entity.getUnitName());
            excel.setNumber(PdosUtils.getNumber(entity.getNumber(), businessSet));
            excel.setAmount(PdosUtils.getPrice(entity.getAmount(), businessSet));
            excel.setAvgAmount(PdosUtils.getPrice(entity.getAmount().divide(entity.getNumber(),4, RoundingMode.HALF_UP), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String fileName = "进货退货报表按商品按供应商表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsRefundAnalysisGoodsProvider.class);
            sheet1.setSheetName("进货退货报表按商品按供应商表信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName, new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsRefundAnalysis:export")
    @RequestMapping("/inGoodsRefundAnalysis/exportProviderDetailDataByOrder")
    public ResponseData exportProviderDetailDataByOrder(String providerId,
                                                      String storeId,
                                                      String houseId,
                                                      String date1,
                                                      String date2,
                                                      String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsRefundVo> list = inGoodsRefundService.listVo(company.getId(),
                null,
                null,
                false,
                Utils.genLikeData(search),
                null,
                null,
                null,
                null,
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                null,
                null,
                Utils.genDate(date1),
                Utils.genDate(date2),
                null,
                providerId,
                houseId,
                null);

        List<ExcelInGoodsRefundAnalysisProviderOrder> excelList = new ArrayList<>();
        for (InGoodsRefundVo entity : list) {
            ExcelInGoodsRefundAnalysisProviderOrder excel = new ExcelInGoodsRefundAnalysisProviderOrder();
            excel.setNo(entity.getNo());
            excel.setDate(DateUtils.getDate(entity.getDate(), DateUtils.YYYY_MM_DD));

            String goods = "";
            if (!Utils.isEmpty(entity.getInGoodsRefundGoodsVoList())) {
                for (InGoodsRefundGoodsVo vo : entity.getInGoodsRefundGoodsVoList()) {
                    goods += vo.getGoodsVo().getName() + "、";
                }
            }

            if (!Utils.isEmpty(goods)) {
                goods = goods.substring(0, goods.length() - 1);
            }
            excel.setGoods(goods);
            excel.setAmount(PdosUtils.getPrice(entity.getOldTotalAmt(), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String fileName = "进货退货报表按供应商按单据表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsRefundAnalysisProviderOrder.class);
            sheet1.setSheetName("进货退货报表按供应商按单据表信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName, new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("inGoodsRefundAnalysis:export")
    @RequestMapping("/inGoodsRefundAnalysis/exportOrderDetailData")
    public ResponseData exportOrderDetailData(String date,
                                              String storeId,
                                              String houseId,
                                              String search) {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        BusinessSet businessSet = getBusinessSet();
        List<InGoodsRefundVo> list = inGoodsRefundService.listVo(company.getId(),
                null,
                null,
                false,
                Utils.genLikeData(search),
                null,
                null,
                null,
                null,
                null,
                null,
                PdosUtils.getViewOtherStoreId(userVo,storeId),
                null,
                null,
                null,
                null,
                Utils.genDate(date),
                null,
                houseId,
                null);


        List<ExcelInGoodsRefundAnalysisOrderDetail> excelList = new ArrayList<>();
        for (InGoodsRefundVo entity : list) {
            ExcelInGoodsRefundAnalysisOrderDetail excel = new ExcelInGoodsRefundAnalysisOrderDetail();
            excel.setNo(entity.getNo());
            excel.setDate(DateUtils.getDate(entity.getDate(), DateUtils.YYYY_MM_DD));

            String goods = "";
            if (!Utils.isEmpty(entity.getInGoodsRefundGoodsVoList())) {
                for (InGoodsRefundGoodsVo vo : entity.getInGoodsRefundGoodsVoList()) {
                    goods += vo.getGoodsVo().getName() + "、";
                }
            }

            if (!Utils.isEmpty(goods)) {
                goods = goods.substring(0, goods.length() - 1);
            }
            excel.setGoods(goods);
            excel.setAmount(PdosUtils.getPrice(entity.getOldTotalAmt(), businessSet));
            excel.setOtherAmt(PdosUtils.getPrice(entity.getOtherAmt(), businessSet));
            excel.setTax(PdosUtils.getPrice(entity.getTax(), businessSet));
            excelList.add(excel);
        }

        String fileName = "进货退货报表按单据详情表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path + fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            Sheet sheet1 = new Sheet(1, 0, ExcelInGoodsRefundAnalysisOrderDetail.class);
            sheet1.setSheetName("进货退货报表按单据详情表信息");
            writer.write(excelList, sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName, new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("导出失败");
        }
    }
}
